/**
 * 功能描述 表单
 */

define(function (require, exports, module){
	var Form = require("form");
	var aw = require("ajaxwrapper");
	var store =require("store");
	var Dialog = require("dialog");
	var activeUser = store.get("user");
	
	var instance = {
		initComponent: function (params,widget){
            var form=new Form({
				parentNode:".J-form",
				show:false,
				saveaction:function(){
        			var data = form.getData();
					aw.saveOrUpdate("api/costprice/save",data,function(data){
						widget.get("grid").refresh();
						widget.show([".J-grid"]).hide([".J-form"]);
						widget.get("subnav").show(["add","search","status"]);//.hide(["return"])
					});
        			
        		},
        		cancelaction : function(){
           			     widget.show([".J-grid"]).hide([".J-form"]);
    					 widget.get("subnav").show(["add","search","status"]);//.hide(["return"])
    					 widget.get("grid").refresh();
        		},
				model:{
					id:"form",
					layout: "2_1",
					items:[{
						name:"pkCostPrice",
						type:"hidden",
					},{
						name:"version",
						defaultValue:"0",
						type:"hidden"
					},{
						name:"travelContract",
						type:"hidden"
					},{
						name:"memberCertificates",
						label:"权证号",
						validate:["required"],
						events:{
							"blur" : function(e){
								if(e.target.value){
									aw.ajax({
										url:"api/travelcontract/searchByMemberCertificates",
										data:{
											s:e.target.value,
											searchProperties:"memberCertificates",
//											fetchProperties:"travelContract.pkTravelContract,travelContract.contactNo,travelContract.version,travelContract.signUpDate," +
//											"travelContract.unit.*,travelContract.member.personalInfo.name,travelContract.member.personalInfo.idNumber,"
										},
										success: function(data){
											if(data==null){
												Dialog.alert({
								    				content:"此权证号不存在。请核对权证号！"
								            	});
											}else{
												form.setValue("name",data.perName==null?null:data.perName);
												form.setValue("idNumber",data.perId==null?null:data.perId);
												form.setValue("travelContract",data.pkTravelContract);
											}
										}
									});
								}
							}
						}
					},{
						name:"name",
						label:"姓名",
						validate:["required"]
					},{
						name:"idNumber",
						label:"身份证号",
						validate:["required"]
					},{
						name:"flatCost",
						label:"金额",
						validate:["required"],
						events:{
							"blur" : function(e){
								var flatCost = e.target.value;
								if(flatCost){
									defaultValue :Number(flatCost).toFixed(2);
								}
							}
						},
					},{
						name:"operateDate",
						label:"办理日期",
						foramt:"date",
						defaultValue:moment().format("YYYY-MM-DD"),
						validate:["required"]
					},{
						name:"cause",//手动填入
						label:"原因",
						validate:["required"],
						exValidate: function(value){
							  var reg = /^[\u4E00-\u9FA5A-Za-z0-9\-\/\\\.\(\)\~\<\>\[\]\{\}\（\）\，\,]{1,15}$/;
	                          if (!reg.test(value)) {
	                                return "只能输入数字、字符、字母和汉字且不能超过15个字!";
	                            } else {
	                                return true;
	                            }
						 }
					},{
						name:"flatCostStatus",
						label:"状态",
						type:"select",
						options:[{
							key:"Nocharge",
							value:"待收费"
						},{
							key:"Done",
							value:"已收费"
						}],
						validate:["required"],
						defaultValue:"Done"
					}]
				}
			});
			widget.set("form", form);
		}
	};
	module.exports = instance;
});